package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.admin.api.feign.RemoteDeptService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.education.convert.EduLearningRecordConvert;
import com.engine4cloud.tdf.education.dto.create.EduLearningRecordCreateDTO;
import com.engine4cloud.tdf.education.dto.query.EduLearningRecordAppQueryDTO;
import com.engine4cloud.tdf.education.dto.query.EduLearningRecordQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduLearningRecordResultDTO;
import com.engine4cloud.tdf.education.entity.EduLearningRecordEntity;
import com.engine4cloud.tdf.education.mapper.EduLearningRecordMapper;
import com.engine4cloud.tdf.education.service.EduLearningRecordService;
import com.engine4cloud.tdf.education.service.EduStudyTaskService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 学习记录表
 *
 * @author wenjie
 * @date 2024-08-15 15:06:54
 */
@Service
public class EduLearningRecordServiceImpl extends ServiceImpl<EduLearningRecordMapper, EduLearningRecordEntity> implements EduLearningRecordService {
    @Resource
    private RemoteDeptService remoteDeptService;

    @Resource
    private EduStudyTaskService taskService;

    @Override
    public PageInfo<EduLearningRecordResultDTO> pageList(Params pageable, EduLearningRecordQueryDTO dto) {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(dto.getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        return Pages.convert(this.baseMapper.pageList(Pages.page(pageable), dto, deptIdList));
    }

    @Override
    public PageInfo<EduLearningRecordResultDTO> recordPageList(EduLearningRecordAppQueryDTO dto) {
        IPage<EduLearningRecordEntity> page = super.page(Pages.page(dto), wrapper(dto));
        return Pages.convert(page, EduLearningRecordConvert.INSTANCE::convert);
    }

    @Override
    public EduLearningRecordResultDTO getById(Long id) {
        return EduLearningRecordConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(EduLearningRecordCreateDTO dto) {
        Optional.ofNullable(taskService.findByCode(dto.getTaskCode()))
                .orElseThrow(() -> new ValidateCodeException("学习任务不存在"));
        TdfUser user = SecurityUtils.getUser();
        return super.save(EduLearningRecordConvert.INSTANCE.create(dto, user.getTenantId()));
    }

    private LambdaQueryWrapper<EduLearningRecordEntity> wrapper(EduLearningRecordAppQueryDTO query) {
        LambdaQueryWrapper<EduLearningRecordEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(EduLearningRecordEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(EduLearningRecordEntity::getCode, v));
        Queries.accept(query.getTaskCode(), v -> wrapper.eq(EduLearningRecordEntity::getTaskCode, v));
        Queries.accept(query.getStudentCode(), v -> wrapper.eq(EduLearningRecordEntity::getStudentCode, v));
        Queries.accept(query.getStudentName(), v -> wrapper.like(EduLearningRecordEntity::getStudentName, v));
        Queries.accept(query.getStartTime(), v -> wrapper.gt(EduLearningRecordEntity::getStartTime, v));
        Queries.accept(query.getEndTime(), v -> wrapper.le(EduLearningRecordEntity::getEndTime, v));
        wrapper.orderByDesc(EduLearningRecordEntity::getCreateTime);
        return wrapper;
    }
}